from UI.mainwindow import *
from UI.show_tab_controller import *
from UI.show_tab_sensor import *
from UI.show_tab_com import *
from UI.show_tab_net import *
from manager.NetManager import NetManager
from PyQt5.QtWidgets import QMainWindow

class MainWindow(QMainWindow):
    def __init__(self, name='Dlg'):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.init_ui(name)
        pass

    def __del__(self):
        pass

    def init_ui(self, name):
        self.ui.setupUi(self)
        self.setWindowTitle(name)
        self.ui.tabWidget.setCurrentIndex(1)
        self.ui.tabWidget.setTabEnabled(0, False)
        self.init_connect()
        TabNet().init_ui(self.ui)
        TabController().init_ui(self.ui)
        TabSensor().init_ui(self.ui)
        TabCom().init_ui(self.ui)
        pass

    def init_connect(self):
        self.ui.cobx_type.currentIndexChanged.connect(self.cmbox_type_change)

    def cmbox_type_change(self):
        cur_index = self.ui.cobx_type.currentIndex()
        if cur_index == NET_TYPE.TCP_SERVER.value or \
                cur_index == NET_TYPE.UDP.value:
            self.ui.ledit_ip.setText(NetManager.get_host_ip())
        # elif cur_index == NET_TYPE.TCP_CLIENT.value:
        #     pass
        else:
            self.ui.ledit_ip.setText("")
            pass

    def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
        Common().set_status_adc_io(False)
        if comManager().com_port is not None:
            TabSensor().quit_debug()
            TabController.disconnect_station()
        pass




